"""
演示全国疫情地图构建
"""
import json
from pyecharts.charts import  Map
from pyecharts.options import *
from pyecharts.options import VisualMapOpts

#打开文件
f = open("C:/Users/86131/Desktop/课程/python资料/资料/第1-12章资料/资料/可视化案例数据/地图数据/疫情.txt","r",encoding="UTF-8")
data = f.read()
#关闭文件
f.close()
#获取每个省份的数据
data_dict = json.loads(data)
province_data_list = data_dict["areaTree"][0]["children"]
#组装每个省份和确定人数为元组,并各个省的数据都封装如列表内
# 新建一个列表用来存储个省份信息
data_list = []
for province_data in province_data_list:
    province_name = province_data["name"]+"省"
    province_confirm= province_data["total"]["confirm"]
    data_list.append((province_name,province_confirm))
print(data_list)
#创建地图对象
map = Map()

#添加数据
map.add("各省份确诊人数",data_list,"china")
#设置全局配置,定制分段视觉映射
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"),
    visualmap_opts=VisualMapOpts(
        is_show=True,
        is_piecewise=True,
        pieces=[
            {"min":1,"max":99,"label":"人数为1-99","color":"#CCFFFF"},
            {"min":100,"max":500,"label":"人数为100-500","color":"#FFFF99"},
            {"min":501,"max":4999,"label":"人数为501-4999","color":"#FF9966"},
            {"min":5000,"max":10000,"label":"人数为5000-10000","color":"#FF6666"},
            {"min":10001,"label":"人数为10001以上","color":"#990033"}
        ]
    )
)
#绘图
map.render("全国疫情地图.html")




